//
//  EpointGetFeedBackListTask.m
//  EpointFrame5
//
//  Created by 李亚春 on 14-1-21.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointGetFeedBackListTask.h"
#import "EpointWebInfoFeedBackModel.h"

@implementation EpointGetFeedBackListTask

-(void)execute
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *ValidateData = [EpointBizTools getToken];
    NSString *wsNamespace = NSLocalizedString(@"Epoint_DNet_WS_NameSpace", @"");
    NSString *url = self.getMobileOAWebserviceURL;
    NSString *userguid = [userDefaults objectForKey:Epoint_User_UserGuid];
    NSString *InfoGuid = [params objectForKey:@"InfoGuid"];
    NSString *CategoryGuid = [params objectForKey:@"CategoryGuid"];
    
    NSString *ParasXml = @"<![CDATA[<?xml version=\"1.0\" encoding=\"gb2312\"?><paras><UserGuid>%@</UserGuid><InfoGuid>%@</InfoGuid><CategoryGuid>%@</CategoryGuid></paras>]]>";
    
    ParasXml = [NSString stringWithFormat:ParasXml,userguid,InfoGuid,CategoryGuid];
    
    //开始请求,判断网络是否可用
    if (![self isNetWorkAvailable])
    {
        [self userCache:ParasXml message:@"网络请求失败，请检查网络配置！"];
    }
    else
    {
        //请求接口
        EpointWebServiceUtil *ws = [[EpointWebServiceUtil alloc]initWithUrl:url namespace:wsNamespace method:@"WebInfo_GetFeedBackView"];
        [ws addParameterForString:@"ValidateData" value:ValidateData];
        [ws addParameterForString:@"ParasXml" value:ParasXml];
        NSString *bs =  ws.start;
        
        NSMutableArray *result = [self validateXMLWS2:bs];
        NSString *msg = [result objectAtIndex:1];
        if ([[result objectAtIndex:0]isEqualToString:@"1"]) {
            [DBFrameService setCacheValue:ParasXml value:bs];
            [self exeSuccessWithData:[self getLst:bs]];
        }
        else
        {
            [self userCache:ParasXml message:msg];
        }
    }
}

-(void)userCache:(NSString *)key message:(NSString *)msg
{
    //获取缓存数据
    NSString *cacheBS = [DBFrameService getCacheValue:key];
    
    //判断是否存在缓存
    if (cacheBS.length>0) {
        [self exeSuccessWithData:[self getLst:cacheBS]];
    }
    else
    {
        [self exeFailureWithMessage:msg];
    }
}

-(NSMutableArray *)getLst:(NSString *)bs
{
    NSString *xmldata = [EpointStringUtil getXMLFormatedAttributeBsWithTag:bs xmlTag:@"FeedBackList"];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:xmldata  options:0 error:nil];
    GDataXMLElement *rootElement = [doc rootElement];
    NSArray *arrays = [rootElement elementsForName:@"FeedBack"];
    NSMutableArray *listmp = [[NSMutableArray alloc]init];
    for (GDataXMLElement *arr in arrays)
    {
        EpointWebInfoFeedBackModel *m = [[EpointWebInfoFeedBackModel alloc]init];
        GDataXMLElement *FeedBackGuid = [[arr elementsForName:@"FeedBackGuid"] objectAtIndex:0];
        m.FeedBackGuid = [FeedBackGuid stringValue];
        
        GDataXMLElement *DispName = [[arr elementsForName:@"DispName"] objectAtIndex:0];
        m.DispName = [DispName stringValue];
        
        GDataXMLElement *UserGuid = [[arr elementsForName:@"UserGuid"] objectAtIndex:0];
        m.UserGuid = [UserGuid stringValue];
        
        GDataXMLElement *BackTime = [[arr elementsForName:@"BackTime"] objectAtIndex:0];
        m.BackTime = [BackTime stringValue];
        
        GDataXMLElement *FeedBackContent = [[arr elementsForName:@"FeedBackContent"] objectAtIndex:0];
        m.FeedBackContent = [FeedBackContent stringValue];
        
        [listmp addObject:m];
    }
    
    return listmp;
}

@end
